<template>
  <div class="container-box">
    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input placeholder="请输入角色名..." v-model="queryForm.query" clearable></el-input>
      </el-col>
      <el-button type="primary" :icon="Search" @click="initRoleList">搜索</el-button>
      <el-button type="success" :icon="DocumentAdd" @click="handleDialogValue()">新增</el-button>
      <!-- <el-popconfirm title="您确定批量删除这些记录吗？" @confirm="handleDelete(null)">
        <template #reference>
          <el-button type="danger" :disabled="delBtnStatus" :icon="Delete">批量删除</el-button>
        </template>
</el-popconfirm> -->
    </el-row>

    <el-table :data="tableData" stripe style="width: 100%" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" /> -->
      <el-table-column prop="name" label="角色名" align="center" />
      <el-table-column prop="code" label="权限字符" align="center" />
      <el-table-column prop="status" label="状态？" align="center">
        <template v-slot="{ row }">
          <el-switch v-model="row.status" class="ml-2" inline-prompt @change="statusChangeHandle(row)" active-text="正常"
            inactive-text="禁用" :active-value="1" :inactive-value="0"
            style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="user_count" label="关联用户量" align="center" />
      <el-table-column prop="create_time" label="创建时间" align="center" />
      <el-table-column prop="update_time" label="更新时间" align="center" />
      <el-table-column prop="remark" label="备注" />
      <el-table-column prop="action" label="操作" fixed="right" align="center">
        <template v-slot="scope">
          <el-button-group class="ml-3" size="small">
            <el-tooltip content="分配权限" placement="top" effect="customized">
              <el-button type="primary" @click="handleMenuDialogValue(scope.row.id)">
                <el-icon><svg-icon icon="permission" /></el-icon>
              </el-button>
            </el-tooltip>

            <el-button v-if="scope.row.code != 'superAdmin'" type="success" :icon="Edit"
              @click="handleDialogValue(scope.row.id)" />
            <el-popconfirm v-if="scope.row.code != 'superAdmin'" title="您确定要删除这条记录吗？"
              @confirm="handleDelete(scope.row.id)">
              <template #reference>
                <el-button type="danger" :icon="Delete" />
              </template>
            </el-popconfirm>
          </el-button-group>

        </template>
      </el-table-column>
    </el-table>

    <el-pagination v-model:currentPage="queryForm.pageNum" v-model:page-size="queryForm.pageSize" size="small"
      :page-sizes="[10, 20, 50]" layout="total, sizes, prev, pager, next, jumper" :total="total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />

    <Dialog v-model="dialogVisible" :dialogVisible="dialogVisible" :id="id" :dialogTitle="dialogTitle"
      @initRoleList="initRoleList"></Dialog>
    <MenuDialog v-model="menuDialogVisible" :menuDialogVisible="menuDialogVisible" :id="id"
      @initRoleList="initRoleList">
    </MenuDialog>
  </div>
</template>

<script setup>
import { Search, Delete, DocumentAdd, Edit, Tools, RefreshRight } from '@element-plus/icons-vue'
import { ref } from 'vue'
import api, { getServerUrl } from "@/util/request";
import { ElMessage, ElMessageBox } from 'element-plus'
import Dialog from './components/dialog.vue'
import MenuDialog from './components/menuDialog.vue'

const id = ref(-1)
const dialogVisible = ref(false)
const dialogTitle = ref('')

const menuDialogVisible = ref(false)

const handleMenuDialogValue = (roleId) => {
  if (roleId) {
    id.value = roleId;
  }
  menuDialogVisible.value = true
}

const handleDialogValue = (roleId) => {
  if (roleId) {
    id.value = roleId;
    dialogTitle.value = "角色修改"
  } else {
    id.value = -1;
    dialogTitle.value = "角色添加"
  }
  dialogVisible.value = true
}
const queryForm = ref({
  query: '',
  pageNum: 1,
  pageSize: 10
})

const total = ref(0)

const tableData = ref([])

const multipleSelection = ref([])

const delBtnStatus = ref(true)


const handleSelectionChange = (selection) => {
  console.log("勾选了:", selection)
  multipleSelection.value = selection;
  delBtnStatus.value = selection.length == 0;
}


const handleDelete = async (id) => {
  var ids = []
  if (id) {
    ids.push(id)
  } else {
    multipleSelection.value.forEach(row => {
      ids.push(row.id)
    })
  }
  const res = await api.del("role/action", ids)
  if (res.data.code == 200) {
    ElMessage({
      type: 'success',
      message: '执行成功!'
    })
    initRoleList();
  } else {
    ElMessage({
      type: 'error',
      message: res.data.msg,
    })
  }
}

const initRoleList = async () => {
  const res = await api.post("role/search", queryForm.value)
  tableData.value = res.data.roleList;
  total.value = res.data.total;
}

const statusChangeHandle = async (row) => {
  let res = await api.post("role/status", { id: row.id, status: row.status });
  if (res.data.code == 200) {
    ElMessage({
      type: 'success',
      message: '执行成功!'
    })
  } else {
    ElMessage({
      type: 'error',
      message: res.data.msg,
    })
    initUserList();
  }
}

initRoleList();

const handleSizeChange = (pageSize) => {
  queryForm.value.pageNum = 1;
  queryForm.value.pageSize = pageSize;
  initRoleList();
}

const handleCurrentChange = (pageNum) => {
  queryForm.value.pageNum = pageNum;
  initRoleList();
}

</script>

<style lang="scss" scoped>
.header {
  padding-bottom: 16px;
  box-sizing: border-box;
}

.el-pagination {
  float: right;
  padding: 10px;
  box-sizing: border-box;
}

::v-deep th.el-table__cell {
  word-break: break-word;
  background-color: #f8f8f9 !important;
  color: #515a6e;
  height: 40px;
  font-size: 13px;
}

.el-tag--small {
  margin-left: 5px;
}
</style>
